package qingke.mybatis;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import test.test1.mybatis.pojo.MyClass;
import test.test1.mybatis.pojo.Student;
import test.test1.mybatis.pojo.User;
import test.test1.mybatis.pojo.User1;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
    	
    	//mybatis的配置文件
    	String resource = "mybatis_conf.xml";
    	
    	//使用类加载器加载mabatis的配置文件（他也加载相关联的映射文件）
    	InputStream is = App.class.getClassLoader().getResourceAsStream(resource);
    	
    	//构建sqlSession的工厂
    	SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is,"development");
    	
    	//创建一个能执行映射文件中的sqlsession
    	SqlSession session = sessionFactory.openSession();
    	
//        System.out.println( "Hello World!" );

    	String statement = "test.test1.mybatis.mapping.studentMapper.getStudentById";
    	String statement1 = "test.test1.mybatis.mapping.studentMapper.selectMyclassById";
    	Student student = session.selectOne(statement, 1);
    	System.out.println(student);
    	MyClass clazz = session.selectOne(statement1, 1);
    	System.out.println(clazz);
    	
//    	
//    	String statement = "test.test1.mybatis.mapping.userMapper.getAllUsers";
//    	List<User> users = session.selectList(statement);
//    	for (User user : users) {
//			System.out.println(user);
//		}
//    	
//    	statement = "test.test1.mybatis.mapping.userMapper.getUserById";
//    	User user = session.selectOne(statement,1);
//    	System.out.println("getUserById:"+user);
//    	
//    	User user1 = new User();
//    	user1.setUserId(10);
//    	user1.setName("zz");
//    	user1.setCode("zz");
//    	user1.setPassword("123");
//    	addNewUser(session,user1);
//    	session.close();
//    	
    	
/*    	
    	String statement1 = "test.test1.mybatis.mapping.user1Mapper.getAllUsers";
    	List<User1> users1 = session.selectList(statement1);
    	for (User1 user1 : users1) {
			System.out.println(user1);
		}
    	
    	statement1 = "test.test1.mybatis.mapping.user1Mapper.getUserById";
    	User1 user1 = session.selectOne(statement1,1);
    	System.out.println("getUserById:"+user1);
    	session.close();*/
    }
    private static void addNewUser(SqlSession session,User user){
		String statement = "test.test1.mybatis.mapping.userMapper.insertUser";
		System.out.println("before="+user);
		int count = session.insert(statement, user);
		session.commit();
		System.out.println("insert count="+count);
		System.out.println("after="+user);
	}
}
